<?php
namespace app\api\controller\order;

use app\api\controller\Controller;
use app\common\library\easywechat\AppMp;
use app\common\library\easywechat\WxPay;
use app\common\model\car\CarPurpose;
use app\common\model\order\CarOrder as CarOrderModel;
use app\common\model\order\CarOrderPlan;
use think\facade\Db;

/**
 * 需求下单
 */
class CarOrder extends Controller
{
    /*
     * 获取车主接单列表
     * */
    public function owner_order()
    {
        $data = $this->request->param();
        $user = $this->getUser();
        $model = new CarOrderModel;
        $data['take_user_id'] = $user['user_id'];
        $count = $model->where(array('is_delete'=>0,'take_user_id'=>$user['user_id']))->count();
        $list = $model->get_owner_list($data);
        return $this->renderSuccess('获取成功',['list'=>$list,'count'=>$count]);
    }

    /*
     * 用户需求订单
     * */
    public function user_order()
    {
        $data = $this->request->param();
        $user = $this->getUser();
        $model = new CarOrderModel;
        $data['user_id'] = $user['user_id'];
        $list = $model->get_user_list($data);
        return $this->renderSuccess('获取成功',['list'=>$list]);
    }

    /*
     * 下单
     * */
    public function add()
    {
        $data = $this->request->param();

        $user = $this->getUser();
        $model = new CarOrderModel;
        $res = $model->add($data,$user);
        if($res){
            return $this->renderSuccess('下单成功',['id'=>$res]);
        }else{
            return $this->renderError('下单失败！');
        }
    }

    /*
     * 获取标签
     * */
    public function get_purpose()
    {
        $model = new CarPurpose();
        $list = $model->select()->toArray();
        return $this->renderSuccess('',['list'=>$list]);
    }

    /*获取订单详情*/
    public function get_details()
    {
        $id = $this->request->param('id');
        $model = new CarOrderModel;
        $res = $model->details($id);
        $user = $this->getUser();
        if($res['owner_status']<1 && $res['status']==1 && $user['classify']==1){
            //添加浏览次数
            $model->where(array("id"=>$id))->update(array('click'=>$res['click']+1));
            //添加已读
            $re = Db::name("message_status")->where(array('user_id'=>$user['user_id']))->find();
            if(!$re){
                $data = array(
                    'user_id'=>$user['user_id'],
                    'order_id'=>$res['id']
                );
                Db::name("message_status")->insertGetId($data);
            }
        }
        return $this->renderSuccess('',['info'=>$res]);
    }

    //删除订单
    public function del_order()
    {
        $id = $this->request->param('id');
        $model = new CarOrderModel;
        $model->where(array('id'=>$id))->update(array('is_delete'=>1));
        //更改在用车辆状态
        $model = new CarOrderPlan();
        $model->where(array('order_id'=>$id))->update(array('status'=>2));
        return $this->renderSuccess('删除成功！');
    }

    //取消退单
    public function cancel_chargeback()
    {
        $id = $this->request->param('id');
        $type = $this->request->param('type');
        if($type==1){
            $data = array(
                'status'=>1
            );
        }else{
            $data = array(
                'owner_status'=>1
            );
        }
        $model = new CarOrderModel;
        $model->where(array('id'=>$id))->update($data);

        return $this->renderSuccess('修改成功！');
    }
    /*接单*/

    public function receiving_orders()
    {
        $data = $this->request->param();
        $user = $this->getUser();
        $model = new CarOrderModel();

        $res = $model->receivingOrders($data,$user);
        if($res){
            return $this->renderSuccess('接单成功！',['res'=>$res]);
        }else{
            return $this->renderError($model->getError());
        }

    }

    //支付押金
    public function order_pay()
    {
        $id = $this->request->param('order_id');
        $type = $this->request->param('type');
        $password = $this->request->param('password');
        $user = $this->getUser();
        $model = new CarOrderModel();
        $res = $model->pay($id,$user,$type,$password);
        if($res){
            return $this->renderSuccess('支付成功！',['res'=>$res]);
        }else{
            return $this->renderError($model->getError());
        }
    }
    //编辑订单
    public function edit_car_order()
    {
        $data = $this->request->param();
        $user = $this->getUser();
        $model = new CarOrderModel();
        $res = $model->edit($data);
        if($res){
            return $this->renderSuccess('修改成功！',['id'=>$data['id']]);
        }else{
            return $this->renderError('修改失败！');
        }


    }


    //首页获取接单列表
    public function get_order_list()
    {
        $data = $this->request->param();
        $model = new CarOrderModel();
        $list = $model->get_list_days($data);

        return $this->renderSuccess('获取成功！',['list'=>$list]);
    }

    //首页订单数
    public function get_order_count()
    {
        $order_type = $this->request->param('order_type');
        if($order_type){
            $where[] = ['order_type','=',$order_type];
        }
        $model = new CarOrderModel();
        $where[] = ['status','=',1];
        $where[] = ['owner_status','=',-1];
       //今日

        $times = $model->date_time(1);
        $where1[] = ['create_time','between',$times];

        //三日

        $times2 = $model->date_time(2);
        $where2[] = ['create_time','between',$times2];
        //一个月内
        $times3 = $model->date_time(6);
        $where3[] = ['create_time','between',$times3];

        $count = $model->where($where)->where($where1)->count();
        $count2 = $model->where($where)->where($where2)->count();
        $count3 = $model->where($where)->where($where3)->count();
        return $this->renderSuccess('获取成功！',['day_count'=>$count,'tday_count'=>$count2,'month_count'=>$count3]);
    }

    public function get_order_count_total()
    {
        $model = new CarOrderModel();
        $where[] = ['status','=',1];
        $where[] = ['owner_status','=',-1];
        $where[] = ['is_delete','=',0];
        $arr = [
            '1'=>0,
            '2'=>0,
            '3'=>0
        ];
        $list = $model->field("count(`id`) as num,order_type")->where($where)->group("order_type")->select()->toArray();
     
        foreach ($arr as $k=>$v){
            foreach($list as $v2){
                if($k==$v2['order_type']){
                    $arr[$k]=$v2['num'];
                }
            }
        }
        return $this->renderSuccess('获取成功！',['count'=>$arr]);
    }

    //分配车辆
    public function plan_car()
    {
        $id = $this->request->param('order_id');
        $car_id = $this->request->param('car_id');
        $model = new CarOrderModel();
        $res = $model->distribution_car($id,$car_id);
        if($res){
            return $this->renderSuccess('安排成功！',['order_id'=>$id]);
        }else{
            return $this->renderError($model->getError());
        }

    }

    //删除分配车辆
    public function del_plan_car()
    {
        $id = $this->request->param('id');

        Db::name('car_order_plan')->where(array('id'=>$id))->delete();
        return $this->renderSuccess('删除成功！');
    }

    //订单已完成
    public function done()
    {
        $id = $this->request->param('order_id');
        $model = new CarOrderModel();
        $res = $model->well_done($id);
        if($res){
            return $this->renderSuccess('操作成功！',['id'=>$id]);
        }else{
            return $this->renderError($model->getError());
        }
    }

    //用户取消订单
    public function user_cancel()
    {
        $id = $this->request->param('order_id');
        $user = $this->getUser();
        $model = new CarOrderModel();
        $res = $model->user_cancel_apply($id,$user);
        if($res){
            return $this->renderSuccess('提交成功！',['id'=>$id]);
        }else{
            return $this->renderError($model->getError());
        }
    }
    //车主退单
    public function owner_cancel()
    {
        $id = $this->request->param('order_id');
        $model = new CarOrderModel();
        $user = $this->getUser();
        $res = $model->owner_cancel_apply($id,$user);
        if($res){
            return $this->renderSuccess('提交成功！',['id'=>$id]);
        }else{
            return $this->renderError($model->getError());
        }
    }

    //开始行程
    public function start_order()
    {
        $id = $this->request->param('order_id');
        $model = new CarOrderModel();
        $res = $model->start_order($id);
        if($res){
            return $this->renderSuccess('操作成功！',['id'=>$id]);
        }else{
            return $this->renderError($model->getError());
        }
    }

    //计算距离
    public function get_distance()
    {
        $origin = $this->request->param('origin');
        $destination = $this->request->param("destination");
        $waypoints = $this->request->param("waypoints");
        $url = "https://restapi.amap.com/v3/direction/driving?origin=".$origin."&destination=".$destination."&waypoints=".$waypoints."&extensions=base&key=cffc2d56d021b2aaff60e2b1a4d865ad";

        $res = curl($url);
        $data = json_decode($res,1);
        if($data['status']==1){
            $distance = intval($data['route']['paths'][0]['distance']/1000);
            return $this->renderSuccess('计算成功',['distance'=>$distance,'cost_price'=>$distance*3]);
        }else{
            return $this->renderError('计算失败！');
        }

    }

    /*
     * 获取车主行程安排
     * */
    public function order_trip()
    {
        $type = $this->request->param('type');
        $car_id = $this->request->param('car_id');
        //获取订单id
        $car_model = new CarOrderPlan();
        $order = $car_model->where(array('car_id'=>$car_id))->column('order_id');
        $order_id = array();
        foreach($order as $v){
            $order_id[] = $v;
        }
        $where[] = ['cot.order_id','in',$order_id];
        if($type==1){
            //未结束
            $where[] = ['co.end_time','>',time()];
        }else{
            //已结束
            $where[] = ['co.end_time','<',time()];
        }

        $list = Db::name('car_order_trip')->alias('cot')
            ->field('cot.id,cot.order_id,cot.content,cot.date_time')
            ->join('jjjshop_car_order co','cot.order_id=co.id','left')
            ->where($where)
            ->order('cot.date_time asc')
            ->paginate(5);
        $lists = $list->items();
        foreach($lists as &$v){
            $content = json_decode($v['content'],1);
            $date_time = array_column($content,'date_time');
            array_multisort($date_time,SORT_ASC,$content);
            $v['content'] = $content;
            if($v['date_time']<time()){
                $v['status'] = 1;
            }
            if($v['date_time']>time()){
                $v['status'] = 2;
            }
            $v['date_time'] = date("Y-m-d",$v['date_time']);
        }
        //dump($lists);die;
        return $this->renderSuccess('获取成功',['list'=>$lists]);

    }



}